<?php


namespace App\Http\Controllers\Warehouse;


use App\Http\Controllers\Controller;
use App\Http\Requests\Warehouse\Consumable\StoreRequest;
use App\Http\Requests\Warehouse\Consumable\UpdateRequest;
use App\Http\Searchs\Warehouse\ConsumableSearch;
use App\Repositories\Consumable;

class ConsumableController extends Controller
{
    /**
     * @var Consumable
     * @author luffyzhao@vip.126.com
     */
    private $consumable;

    /**
     * ConsumableController constructor.
     * @param Consumable $consumable
     * @author luffyzhao@vip.126.com
     */
    public function __construct(Consumable $consumable)
    {
        $this->consumable = $consumable;
    }

    /**
     * @param ConsumableSearch $search
     * @return \Illuminate\Http\JsonResponse
     * @author luffyzhao@vip.126.com
     */
    public function index(ConsumableSearch $search)
    {
        return $this->response(
            $this->consumable->paginate($search->toArray())
        );
    }

    /**
     * @param StoreRequest $request
     * @return \Illuminate\Http\JsonResponse
     * @author luffyzhao@vip.126.com
     */
    public function store(StoreRequest $request)
    {
        return $this->response(
            $this->consumable->create(
                $request->only(['name', 'price', 'weight', 'remark', 'length', 'width', 'height'])
            )
        );
    }

    /**
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function show($id){
        return $this->response(
            $this->consumable->show($id)
        );
    }

    /**
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     * @author luffyzhao@vip.126.com
     */
    public function edit($id)
    {
        return $this->response(
            $this->consumable->find($id)
        );
    }

    /**
     * @param UpdateRequest $request
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     * @throws \Throwable
     * @author luffyzhao@vip.126.com
     */
    public function update(UpdateRequest $request, $id)
    {
        return $this->response(
            $this->consumable->update($id, $request->only(['name', 'price', 'weight', 'remark', 'length', 'width', 'height']))
        );
    }

}
